//
//  QTopUPCell.m
//  QoilPublic
//
//  Created by 杨凯 on 16/7/14.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QTopUPCell.h"
#import "QTopUpView.h"
#import "QTopUpLabel.h"
@interface QTopUPCell ()

@property(nonatomic,strong)NSMutableArray *labelArr;
@property(nonatomic,assign)CGFloat viewHeight;
@end
@implementation QTopUPCell{

    CGFloat h;
}

- (void)setCellWithData:(NSMutableArray *)array{
    self.dataArr = array;
    CGFloat W = (kScreenWidth-20)/3;
    CGFloat H = (kScreenWidth)/3-65;
    CGFloat spanc = 5;//横间距
    CGFloat hspanc = 10;//纵间距
    int count = (int)_dataArr.count;
    int rows = (count)/3 + 1;//下面手动多加了一个按钮，如果不用多加，count需要-1
    
    for (int i=0; i<_dataArr.count +1 ; ++i) {
        QTopUpLabel *label = [[QTopUpLabel alloc] init];
        label.frame = CGRectMake(i%3*(W+spanc)+spanc, i/3*(H+hspanc)+hspanc, W, H);
        label.tag = i;
        label.userInteractionEnabled = YES;
        label.layer.borderWidth = 0.5;
        label.layer.borderColor = [QColorUtility defaultLineColor].CGColor;
        label.layer.cornerRadius = 4.0;
        label.layer.masksToBounds = YES;
        label.backgroundColor = [UIColor whiteColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:14];
        if (i != _dataArr.count) {
            //label.dataDic = _datas[i];
            QTopUpModel *model = [_dataArr objectAtIndex:i];
            label.topModel = model;
            
        }else{
            label.moneylab = @"其他金额";
        }
        UITapGestureRecognizer *pan = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
        [label addGestureRecognizer:pan];
        [self.labelArr addObject:label];
        [self addSubview:label];
    }
    if(_dataArr.count == 1){
        _viewHeight = rows*(H+hspanc)+spanc;
    }else{
        _viewHeight = rows*(H+hspanc)+spanc;
    }
    self.height = _viewHeight;
}

- (void)panAction:(UITapGestureRecognizer*)pan{
    QTopUpLabel *label = (QTopUpLabel *)pan.view;
    for (QTopUpLabel *lab in _labelArr) {
        if(lab.tag == label.tag){
            lab.backgroundColor = [QColorUtility defaultBlueColor];
            lab.lable_1.textColor = [UIColor whiteColor];
            //            lab.lable_2.textColor = [UIColor whiteColor];
            //            lab.lable_3.textColor = [UIColor whiteColor];
            if(self.block){
                self.block(lab.tag);
            }
        }else{
            lab.lable_1.textColor = [UIColor blackColor];
            //            lab.lable_2.textColor = [QColorUtility defaultRedColor];
            //            lab.lable_3.textColor = [QColorUtility defaultDeepGrayColor];
            lab.backgroundColor = [UIColor whiteColor];
            lab.layer.borderWidth = 0.5;
        }
    }
}

-(void)setSelectedBtnWithData:(NSString *)moneyString{
    
    NSLog(@"moneyString:%@",moneyString);
    int money = [moneyString intValue];
   
        for (int i = 0; i<_labelArr.count; i++) {
            QTopUpLabel *label  = [_labelArr objectAtIndex:i];
            NSLog(@"label:%@",label.topModel.payAmount);
            int num = [label.topModel.payAmount intValue];
            if (num == money) {
                label.backgroundColor = [QColorUtility defaultBlueColor];
                label.lable_1.textColor = [UIColor whiteColor];

            }else{
                
                label.lable_1.textColor = [UIColor blackColor];
                label.backgroundColor = [UIColor whiteColor];
                label.layer.borderWidth = 0.5;
            }
        }
}

ARRAY_INIT(labelArr);
ARRAY_INIT(dataArr);
@end
